package com.lhy.ltss.config;

import com.lhy.ltss.dao.LogDao;
import com.lhy.ltss.tag.HasLogin;
import com.lhy.ltss.tag.HasRole;
import com.lhy.ltss.session.UserManage;
import com.lhy.ltss.tag.MyLog;
import com.lhy.ltss.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Objects;

/**
 * @description: 根据权限拦截请求
 * @author: 李汇远
 */
@Component
public class SecurityConfig extends HandlerInterceptorAdapter implements WebMvcConfigurer {

    @Autowired
    UserManage userManage;

    @Resource
    LogDao logDao;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler)
            throws Exception {
        // 只对Controller层的方法访问拦截
        if (!(handler instanceof HandlerMethod))
            return true;
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        // @HasLogin拦截
        if (handlerMethod.hasMethodAnnotation(HasLogin.class)){
            if (userManage.isLogin())
                return true;
            else {
                response.getWriter().println(ResultJson.fail("no power!"));
                return false;
            }
        }
        // @HasRole拦截
        if (handlerMethod.hasMethodAnnotation(HasRole.class)){
            HasRole hasRole = handlerMethod.getMethodAnnotation(HasRole.class);
            if (Objects.requireNonNull(hasRole).value().equals(userManage.getRole()))
                return true;
            else {
                response.getWriter().println(ResultJson.fail("no power!"));
                return false;
            }
        }
        return true;
    }

    // 添加日志
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            if (handlerMethod.hasMethodAnnotation(MyLog.class)){
                MyLog myLog = handlerMethod.getMethodAnnotation(MyLog.class);
                // 调用dao接口，插入数据库
                logDao.add(userManage.getUsername(),
                        myLog.value(),
                        Calendar.getInstance().getTime());
            }
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 将拦截器注册到容器中
        registry.addInterceptor(this).addPathPatterns("/**");
    }
}
